#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
    vector<int> addToArrayForm(vector<int> &num, int k)
    {
        vector<int> res;
        int n = num.size();
        for (int i = n - 1; i >= 0; --i)
        {
            int sum = num[i] + k % 10;
            k /= 10;
            if (sum >= 10)
            {
                k++;
                sum -= 10;
            }
            res.push_back(sum);
        }

        // k 剩余位数
        for (; k > 0; k /= 10)
        {
            res.push_back(k % 10);
        }
        reverse(res.begin(), res.end());
        return res;
    }

    vector<int> addToArrayForm_2(vector<int> &num, int k)
    {
        vector<int> res;
        int n = num.size();
        for (int i = n - 1; i >= 0 || k > 0; --i, k /= 10)
        {
            if (i >= 0)
            {
                k += num[i];
            }
            res.push_back(k % 10);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};
int main()
{
    Solution s;
    vector<int> num = {2, 1, 5};
    int k = 806;
    vector<int> ret = s.addToArrayForm(num, k);
    for (auto &v : ret)
    {
        cout << v << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
